clear;
clc;

Lpath={ '100_0109'...
  '100_0497'...
  '112255696146'...
  '20060319_087'...
  'b14pavel013'...
  'b1chesnuttame'...
  'b4nature_animals_land009'...
  'broom07'...
  'buggy_005'...
  'carriage'...
  'Carrigafoyle_Castle_Ext'...
  'chaom38'...
  'culzeancastle'...
  'DSC01236'...
  'DSCF0034_l'...
  'DSCF3583'... %40
  'DSCN0756'...
  'DSCN2064'...
  'DSCN2154'...
  'DSCN6805'...
  'DSC_0959'... 
  'IMG_1516'...    %60
  'IMG_2528'...
  'IMG_2577'...
  'London_Zoo3'...
  'osaka060102_DYJSN071'...
  'PIC106470172014'...
  'PIC1092515922117'...
  'San_Andres_130'...
  'SG_01_IMG_1943_tratada'...
  'Skookumchuk_starfish1'...
  'snow2_004'...
  'tendrils'...
  'windowCN_0078'...
  'yokohm060409_DYJSN191'};

DBpath = 'D:\ImageSource\Weizmann_Seg_DB_1obj\';
for j=1:length(Lpath)
    filepath = strcat(DBpath,Lpath(j),'\src_color\',Lpath(j),'.png');
    I = imread(filepath{1});
    I = rgb2gray(I);
    m=size(I,1);
    n=size(I,2);
    gray=reshape(I,m*n,1);
    options = [2;1000;1e-5;1];
    claNum = 2;
    [uk,Ck] = kmeans(double(gray),claNum,'maxiter',1000,'Display','final','Replicates',2);
    [centerf,uf,objf] = fcm(double(gray),claNum,options);
    [centerkf,ukf,objkf] = kfcmFun(double(gray),claNum,1000,500,2);
    [~,indk] = sort(Ck);
    [~,indf] = sort(centerf);
    [~,indkf] = sort(centerkf);
    for i = 1 : size(uk,1)
        uk(i) = indk(uk(i));
    end
    uf = uf(indf,:);
    ukf = ukf(indkf,:);
    step = fix(256./claNum);
    [~,indexf] = max(uf);
    [~,indexkf] = max(ukf);
    grayk = zeros(size(gray));
    grayf = zeros(size(gray));
    graykf = zeros(size(gray));
    for i = 1 : claNum
        grayk(uk==i,:) = i*step;
        grayf(indexf==i,:) = i*step;
        graykf(indexkf==i,:) = i*step;
    end
    imgk = reshape(grayk,m,n);
    imgf = reshape(grayf,m,n);
    imgkf = reshape(graykf,m,n);
    kpath = strcat(DBpath,Lpath(j),'\kmeans35');
    fpath = strcat(DBpath,Lpath(j),'\fcm35');
    kfpath = strcat(DBpath,Lpath(j),'\kfcm35');
%     cd(bpath{1});
    if ~exist(kpath{1})
        mkdir(kpath{1});
    end
    if ~exist(fpath{1})
        mkdir(fpath{1});
    end
    if ~exist(kfpath{1})
        mkdir(kfpath{1});
    end
    kmeanspath = strcat(kpath{1},'\',Lpath(j),'.png');
    fcmpath = strcat(fpath{1},'\',Lpath(j),'.png');
    kfcmpath = strcat(kfpath{1},'\',Lpath(j),'.png');
    imwrite(uint8(imgk),kmeanspath{1});
    imwrite(uint8(imgf),fcmpath{1});
    imwrite(uint8(imgkf),kfcmpath{1});
end;